<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

session_start();

require_once 'includes/connect.php';
require_once 'includes/function.php';

$task = $_REQUEST["task"];

$db = new myDBS();

$id = $_REQUEST["id"];
$pet_id = $_REQUEST["pet_id"];
$pet_date = $_REQUEST["pet_date"];
$promise_id = $_REQUEST["promise_id"];

$cash = $_REQUEST["cash"];

$return_date = $_REQUEST["return_date"];
$return_amount = $_REQUEST["return_amount"];

$budget_year = $_REQUEST["budget_year"];

switch ($task) {
    case "new":

        $budget_year = date("Y");

        $pet_id = $db->get_running($budget_year);

        $sql = "INSERT INTO tbl_pet_budget (promise_id, pet_id, pet_date)"
                . " VALUES ('$promise_id', '$pet_id', '$pet_date')";

        echo $pet_id;
        $db->runSQL($sql) or die(mysql_error());
        
        
        //$db->runSQL($sql) or die(mysql_error());
        break;

    case "del":
        break;

    case "return":
        $type = "doc";

        $pet_return_id = $db->get_running($budget_year);
        
        $sql = "INSERT INTO tbl_return (return_date, return_amount, type, promise_id, pet_id, pet_return_id, remain_cash) "
                . " VALUES ('$return_date', '$return_amount', '$type', '$promise_id', '$pet_id', '$pet_return_id', '$cash')";

        //echo $sql;
        $db->runSQL($sql) or die(mysql_error());
        
//        $cash_amount = 0;
//        if ($cash) {
//            $sql = "INSERT INTO tbl_return (return_date, return_amount, type, promise_id, pet_id, pet_return_id) "
//                . " VALUES ('$return_date', '$return_amount', 'cash', '$promise_id', '$pet_id', '$pet_return_id')";
//
//        //echo $sql;
//            $db->runSQL($sql) or die(mysql_error());
//            
//            $cash_amount = 0;
//        }

        if ($cash) {
            $sql = "UPDATE tbl_promise_budget SET remain = 0 "
                . " WHERE id = '$promise_id'";
        } else {
            $sql = "UPDATE tbl_promise_budget SET remain = remain - $return_amount "
                . " WHERE id = '$promise_id'";
        }
        
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "cancel":
        $sql = "UPDATE tbl_pet_budget "
                . " SET cancel_date = '$cancel_date', "
                . " cancel_reason = '$cancel_reason', "
                . " status = 'cancel' "
                . " WHERE pet_id = '$id'";
        //echo $sql;

        $db->runSQL($sql) or die(mysql_error());

        break;
}
?>

